package top.zhimingw.rabbitmq;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;
import top.zhimingw.dto.Email;
import top.zhimingw.utils.BeanUtils;
import top.zhimingw.utils.LogUtils;
import top.zhimingw.utils.MailUtils;

/**
 * @ClassName: EmailConsumer
 * @Description:
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/7/14 16:36
 */
@Component
@RabbitListener(queuesToDeclare = @Queue(value = "email",durable = "true",autoDelete = "true"))
public class EmailConsumer {
    private Logger logger = LogUtils.getInstance(EmailConsumer.class);
    
    @Autowired
    protected JavaMailSenderImpl mailSender;
    
    protected MailUtils mailUtils;
    
    /**
    * @Description: 消费消息：发送回复邮件
    * @Param: [message]
    * @return: void
    * @Author: zhimingw
    * @Date: 2021/7/14
    */
    @RabbitHandler
    public void sendEmail(String message){
        if(!StrUtil.isEmpty(message)){
            Email email = BeanUtils.stringToBean(message, Email.class);
            logger.warn("【RabbitMq】发送邮箱的消息，email："+email);
            //发送回复邮件
            mailUtils = new MailUtils(mailSender);
            mailUtils.sendReplyEmail(email.getTitle(),email.getContent(),email.getUrl(),email.getTo());
            logger.warn("【RabbitMq】消费成功");
        }
    }
}
